/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.adv.service;

import java.util.List;

import com.thinkgem.jeesite.modules.hk.adv.entity.HkAllAdv;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.modules.hk.adv.entity.HkTraditionAdv;
import com.thinkgem.jeesite.modules.hk.adv.dao.HkTraditionAdvDao;

/**
 * 传统文化管理Service
 * @author 罗天文
 * @version 2018-03-20
 */
@Service
@Transactional(readOnly = true)
public class HkTraditionAdvService extends CrudService<HkTraditionAdvDao, HkTraditionAdv> {
	@Autowired
	HkAllAdvService hkAllAdvService;
	public HkTraditionAdv get(String id) {
		return super.get(id);
	}
	
	public List<HkTraditionAdv> findList(HkTraditionAdv hkTraditionAdv) {
		return super.findList(hkTraditionAdv);
	}
	
	public Page<HkTraditionAdv> findPage(Page<HkTraditionAdv> page, HkTraditionAdv hkTraditionAdv) {
		return super.findPage(page, hkTraditionAdv);
	}
	
	@Transactional(readOnly = false)
	public void save(HkTraditionAdv hkTraditionAdv) {

		hkTraditionAdv.setTag(null);
		if(null!=hkTraditionAdv.getTags()){
			hkTraditionAdv.setTag(String.join(",", hkTraditionAdv.getTags()));
		}
		super.save(hkTraditionAdv);

		deleteTag(hkTraditionAdv);
		insertTag(hkTraditionAdv);
		deleteCategory(hkTraditionAdv);
		insertCategory(hkTraditionAdv);
		if(hkTraditionAdv.getState().equals("1")) {
			insertAllAdv(hkTraditionAdv);
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkTraditionAdv hkTraditionAdv) {
		super.delete(hkTraditionAdv);
	}


	/**
	 * 删除标签
	 * @param hkTraditionAdv
	 * @return
	 */
	private void deleteCategory(HkTraditionAdv hkTraditionAdv){
		super.dao.deleteCategory(hkTraditionAdv);
	}
	/**
	 * 增加标签
	 * @param hkTraditionAdv
	 * @return
	 */
	private void insertCategory(HkTraditionAdv hkTraditionAdv){
		super.dao.insertCategory(hkTraditionAdv);
	}
	//同步数据
	private void insertAllAdv(HkTraditionAdv hkTraditionAdv){
		String sourceId=hkTraditionAdv.getId();
		HkAllAdv hkAllAdvOld =hkAllAdvService.getBySourceId(sourceId);
		if(null==hkAllAdvOld){
			HkAllAdv hkAllAdv = new HkAllAdv();
			BeanUtils.copyProperties(hkTraditionAdv, hkAllAdv);
			hkAllAdv.setId(null);
			hkAllAdv.setSourceId(hkTraditionAdv.getId());
			//类型  1自媒体 2企业广告3城市广告4传统文化
			hkAllAdv.setType("4");
			hkAllAdvService.save(hkAllAdv);
		}
		else{
			HkAllAdv hkAllAdv =hkAllAdvOld;
			BeanUtils.copyProperties(hkTraditionAdv, hkAllAdv);
			hkAllAdv.setId(hkAllAdvOld.getId());
			hkAllAdvService.save(hkAllAdv);
		}

		hkAllAdvService.deleteTag(hkTraditionAdv.getId(),hkTraditionAdv.getTag());
		hkAllAdvService.insertTag(hkTraditionAdv.getId(),hkTraditionAdv.getTag());
		hkAllAdvService.deleteCategory(hkTraditionAdv.getId(),hkTraditionAdv.getCategory());
		hkAllAdvService.insertCategory(hkTraditionAdv.getId(),hkTraditionAdv.getCategory());
	}
	/**
	 * 删除标签
	 * @param hkTraditionAdv
	 * @return
	 */
	private void deleteTag(HkTraditionAdv hkTraditionAdv){
		super.dao.deleteTag(hkTraditionAdv);
	}
	/**
	 * 增加标签
	 * @param hkTraditionAdv
	 * @return
	 */
	private void insertTag(HkTraditionAdv hkTraditionAdv){
		super.dao.insertTag(hkTraditionAdv);
	}
}